$(function(){
    if(!useInfo){
        layer.load(0); 
        let index = layer.alert('请先登录', {
            icon: 0,
            time: 0
        });
        setTimeout(function () {
            layer.close(index);
            location.href = 'login.html';
        }, 1000);
    }
    // 获取到当前购买的商品信息
    let cartIds = getParam('cartids');
    $.get(baseUrl+'/cart/lists',{userId:useInfo.id,cartIds},(res)=>{
        let html='';
        let totalMoney = 0;
        $('.commodity').remove();
        res.data.forEach((item)=>{
            html +=`<tr class="commodity">
            <th class="content"><img src="${item.goods.goodsThumb}" width="100px" height="100px">
                <p>${item.goods.goodsName} </p>
            </th>
            <th><span>
            ${item.goodsCount}
                </span> 
                </th>
            <th class="price">
            ${item.goods.shopPrice}
            </th>
            <th class="money">
            ${item.goodsCount*item.goods.shopPrice}
            </th>
            <th><a href="javascript:;" class="glyphicon glyphicon-remove">删除
                </a></th>
        </tr>`
            
            totalMoney+= item.goodsCount * item.goods.shopPrice;
        });
        $('.goods tbody').append(html);
        $('.fillin_ft strong').html('￥'+totalMoney+'元')
    })
    // 显示地址信息
    $.get(baseUrl+'/address/lists',{userId:useInfo.id},(res)=>{
        let html = '';
        console.log(res);
        res.data.forEach((item,index)=>{
            html+= `<li ${index ==0?"class='cur'":''}>
            <input type="radio" name="addressId" ${index==0?'checked':''}  value="${item.id}">${item.consignee} ${item.consignee} ${item.provinceName}${item.cityName} ${item.areaName}${item.address}
            <a href="">设为默认地址</a>
            <a href="">编辑</a>
            <a href="">删除</a>
        </li>`
        })
        $('.address_select>ul').html(html);
    })
    // 提交订单
    $('.fillin_ft a').click(function(){
        let data = $('form').serialize();
        data+='&userId='+useInfo.id;
        $.post(baseUrl+'/order/orderNow',data,(res)=>{
            console.log(res);
        })
        return false;
    })
    // return false;
})